<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:value:add']">新增</el-button>
            </el-col>
        </el-row>

        <el-table :data="list">
            <el-table-column label="标签" align="center" prop="triggerLabel" />
            <el-table-column label="值" align="center" prop="triggerValue" />
            <el-table-column label="显示顺序" align="center" prop="orderNum" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, scope.$index)"
                        v-hasPermi="['system:value:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.$index)"
                        v-hasPermi="['system:value:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加或修改【请填写功能名称】对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="90px">
                <el-form-item label="标签" prop="triggerLabel">
                    <el-input v-model="form.triggerLabel" placeholder="请输入标签" />
                </el-form-item>
                <el-form-item label="值" prop="triggerValue">
                    <el-input v-model="form.triggerValue" placeholder="请输入标签" />
                </el-form-item>
                <el-form-item label="显示顺序" prop="orderNum">
                    <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
export default {
    name: "Value",
    props: {
        list: {
            type: Array,
            default: () => ([])
        }
    },
    data() {
        return {
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                triggerId: null,
                orderNum: null,
                triggerValue: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
    },
    methods: {
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                index: null,
                triggerId: null,
                orderNum: null,
                triggerValue: null
            };
            this.resetForm("form");
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加值";
        },
        /** 修改按钮操作 */
        handleUpdate(row, index) {
            this.reset();
            console.log(index)
            this.form = { ...row };
            this.form.index = index;
            this.open = true;
            this.title = "修改值";
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    let list = [...this.list]
                    let { index } = this.form
                    if (index != null) {
                        list[index] = { ...this.form }
                    } else {
                        list.push({ ...this.form })
                    }
                    this.$emit("update:list", list.sort((a,b) => a.orderNum - b.orderNum))
                    this.cancel()
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(index) {
            let list = [...this.list]
            list.splice(index, 1)
            this.$emit("update:list", list)
        },

    }
};
</script>
  